$(function() {
	$("#mobileForm").validate({
		rules : {
			accountCode : {
				required : true,
				mobilePhone : true,
				remote : {
					url : '/account/check',
					data : {
						registerType : 0
					}
				}
			},
			code : 'required'
		},
		messages : {
			accountCode : {
				required : "请输入手机号码",
				mobilePhone : $.validator.format("手机号码格式不正确"),
				remote : "手机号码已经被注册"
			},
			code : {
				required : "请输入验证码"
			}
		},
		submitHandler : function(form) {
			form.submit();
		}
	});
	$("#emailForm").validate({
		rules : {
			accountCode : {
				required : true,
				email : true,
				remote : {
					url : '/account/check',
					data : {
						registerType : 1
					}
				}
			}
		},
		messages : {
			accountCode : {
				required : "请输入邮箱地址",
				email : "不是有效的邮箱地址",
				remote : "邮箱已经被注册"
			}
		},
		submitHandler : function(form) {
            $("#js_email_btn").button("loading");
            $.post(form.action,$(form).serialize(),
                function(data,status){
                    if(data.status == 'ok'){
                        $(".alert-success").fadeIn(1000);
                        $("#js_email_btn").hide();
//                        setTimeout(function(){
//                            $(".alert-success").fadeOut(1000);
//                        },3000);
                    }else{
                        $(".alert-warning").text(data.message).fadeIn(1000);
                        setTimeout(function () {
                            $(".alert-warning").fadeOut(1000);
                        }, 3000);
                        $("#js_email_btn").button("reset");
                    }
                });
		}
	});

	$('.btn-send').click(function() {
		if(!$('#mobile').is('.valid')){
			$('#mobile').focus();
			return;
		}
		sendSMS($('#mobile').val());
		var interval = 180;
		var self = $(this).addClass('disabled').text(interval-- + '秒后重新发送');
		var timeout = setInterval(function() {
			if (interval == 0) {
				clearInterval(timeout);
				self.removeClass('disabled');
				self.text('获取验证码');
				return;
			}
			self.text(interval-- + '秒后重新发送');
		}, 1000);
	});
	
	function sendSMS(mobilePhone){
		console.log('开始发送短信');
        $.get('/account/send/sms?mobilePhone='+mobilePhone+"&sendType=0");
        console.log('发送短信成功');
	}
});